package com.tms.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tms.basic.pojo.domain.Department;
import com.tms.basic.mapper.DepartmentMapper;
import com.tms.basic.service.DepartmentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Override
    public Page<Department> listDepartments(Page<Department> page, Department department) {
        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(department.getDeptCode()), Department::getDeptCode, department.getDeptCode())
              .like(StringUtils.isNotBlank(department.getDeptName()), Department::getDeptName, department.getDeptName())
              .eq(department.getParentId() != null, Department::getParentId, department.getParentId())
              .eq(department.getStatus() != null, Department::getStatus, department.getStatus())
              .orderByAsc(Department::getSort);
        return this.page(page, wrapper);
    }

    @Override
    public boolean createDepartment(Department department) {
        return this.save(department);
    }

    @Override
    public boolean updateDepartment(Department department) {
        return this.updateById(department);
    }

    @Override
    public boolean deleteDepartment(Long id) {
        return this.removeById(id);
    }
}